<template>
  <div>

    <!--头部-->
    <div style="position: sticky;z-index: 8;width: 100%;top: 0;display: block;background-color: white;margin-bottom: -5px">
      <div style="box-shadow: 0 2px 4px rgba(0,0,0,0.28);">
        <div class="front-notice"><i class="el-icon-bell" style="margin-right: 2px"></i>滚动公告：{{ top }}</div>
        <div class="front-header">
          <div class="front-header-left">
            <img src="@/assets/imgs/logo.png" alt="">
            <div class="title">宠喵知道</div>
          </div>
          <div class="front-header-center">

            <div @click="changePath(item.path)"
                 class="menu-item" v-for="item in menus"
                 :key="item.text"
                 :class="{'menu-item-active' :item.path=== $route.path}">
              {{item.text}}</div>
          </div>

          <div style="white-space: nowrap">
            <el-input v-model="name" style="width: 200px; margin-right: 10px; margin-left: 100px" placeholder="请输入猫咪名称"></el-input>
            <el-button @click="search" style="background-color: #f59300;border-color: #f59300;color: white">搜索</el-button>
          </div>

          <div class="front-header-right">
            <div v-if="!user.username">
              <el-button @click="$router.push('/login')">登录</el-button>
              <el-button @click="$router.push('/register')">注册</el-button>
            </div>
            <div v-else>
              <el-dropdown>
                <div class="front-header-dropdown">
                  <img :src="user.avatar" alt="">
                  <div style="margin-left: 10px">
                    <span>{{ user.name }}</span><i class="el-icon-arrow-down" style="margin-left: 5px"></i>
                  </div>
                </div>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item>
                    <a :href="'/front/usercenter?id=' + user.id">  <div>我的主页</div></a>
                  </el-dropdown-item>
                  <el-dropdown-item>
                    <a href="/front/person">  <div>我的账号</div></a>

                  </el-dropdown-item>
                  <el-dropdown-item>
                    <a href="/front/usercats">  <div>我的经验</div></a>
                  </el-dropdown-item>
                  <el-dropdown-item>
                    <a href="/front/usernotebook">  <div>我的笔记</div></a>
                  </el-dropdown-item>

                  <el-dropdown-item>
                    <div  @click="logout">退出</div>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!--主体-->
    <div class="main-body">
      <router-view ref="child" @update:user="updateUser" />
    </div>
  </div>

</template>

<script>

export default {
  name: "FrontLayout",

  data () {
    return {
      name: this.$route.query.name,
      top: '',
      notice: [],
      user: JSON.parse(localStorage.getItem("xm-user") || '{}'),
      menus:[
        { text:'首页',path:'/front/frontHome'},
        { text:'经验',path:'/front/cats'},
        { text:'笔记',path:'/front/notebook'},
        { text:'问答',path:'/front/problem'},
        { text:'新闻',path:'/front/news'},
      ]
    }
  },

  mounted() {
    this.loadNotice()
  },
  methods: {
    goTop() {
      window.scrollTo({
        top: 0,
        behavior: 'smooth'
      });
    },
    search() {
      window.open('/front/cats?name=' + this.name)
      // location.href=('/front/cats?name=' + this.name);
    },

    changePath(path){
      location.href=path;
    },
    loadNotice() {
      this.$request.get('/notice/selectAll').then(res => {
        this.notice = res.data
        let i = 0
        if (this.notice && this.notice.length) {
          this.top = this.notice[0].content
          setInterval(() => {
            this.top = this.notice[i].content
            i++
            if (i === this.notice.length) {
              i = 0
            }
          }, 2500)
        }
      })
    },
    updateUser() {
      this.user = JSON.parse(localStorage.getItem('xm-user') || '{}')   // 重新获取下用户的最新信息
    },
    // 退出登录
    logout() {
      localStorage.removeItem("xm-user");
      this.$router.push("/login");
    },
  }

}
</script>

<style scoped>
  @import "@/assets/css/front.css";

  .menu-item {
    height: 50%;
    width: 35px;
    display: flex;
    align-items: center;
    margin-right: 30px;
    font-size: 16px;
    cursor: pointer;
  }
  .menu-item:hover {
    color: #FFB31A;
    font-weight: bold;
  }
  .menu-item-active {
    color: #FFB31A;
    font-weight: bold;
    border-bottom: 3px solid #FFB31A;
  }
</style>